//onToSearchPagepages/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_cnt:10,
    nowPage: 1,
    navbarActiveIndex: 0,
    navbarTitle: [
      "公告牌",
      "旧物",
      "发布",
      "话题",
      "我的"
    ],
    navbarTitle2: [
      "最新信息",
      "热度排名"
    ],
    swiperImages:[
      "http://static.geek51.cn/images/diandian2.0/01.jpg",
      "http://static.geek51.cn/images/diandian2.0/02.jpg",
      "http://static.geek51.cn/images/diandian2.0/03.jpg",
      "http://static.geek51.cn/images/diandian2.0/04.jpg"
    ],
    index: 0,
    searchContent: '',
    show:true,
    items:[],
    now_page: 1,
    is_campus: 1,
    news:['民大综合社交信息平台', '民大点点QQ端暂时暂停服务', '成绩查询、课表、校园网功能已开放', '民大点点官方群:698661470']
  },

  onLoad: function(options){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    that.onJudgeSendItem(user_id);
    var imageUrl = app.globalData.imageUrl;  //七牛图片路径前缀
    that.setData({
      imageUrl: imageUrl
    })
  },

  onJudgeSendItem: function(user_id){
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'users/' + user_id + '/',
      data: {
        is_judge: 1
      },
      success: res => {
        console.log(res.data);
        if(res.data.error == 0){
          wx.showModal({
            title: '提示',
            content: '为了用户及时联系到您，请前往添加您的QQ',
            showCancel: false,
            success: res => {
              if(res.confirm){
                wx.navigateTo({
                  url: '/pages/my/personalMsg?user_id=' + user_id + '&is_here=1',
                })
              }
            }
          })
        }
      }
    })
  },  

  onShow: function () {
    var that = this;
    var is_campus = wx.getStorageSync('is_campus');
    that.setData({
      is_campus: is_campus
    })
    if(is_campus == null){
      is_campus = 1;
    }
    that.readItemList(that.data.navbarActiveIndex);
  },

  //点击头像，跳到个人信息主页
  onToPersonMsg: function(e){
    var that = this;
    var user_id = e.currentTarget.dataset.user_id;
    wx.navigateTo({
      url: '/pages/my/personalMsg?user_id=' + user_id,
    })
  },

  changeTabbar(e) {
    this.setData({ index: e.currentTarget.dataset.id })
  },

  //button按钮跳转至searchPage 并收集formID
  onToSearchPage: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/index/searchPage',
    })
    var user_id = wx.getStorageSync('user_id');
    var formId = e.detail.formId;
    app.saveFormId(user_id, formId);
  },

  //访问api读取items信息
  readItemList: function (navbarActiveIndex){
    var that = this;
    var is_campus = that.data.is_campus;
    wx.request({
      url: app.globalData.targetUrl + 'items/',
      data: {
        page_cnt: that.data.page_cnt,
        page: that.data.nowPage,
      },
      success: function (res) {
        var items = res.data.data;
        that.setData({
          items: items,
          navbarActiveIndex: navbarActiveIndex
        });
      }
    })
  },

  //点击物品跳转至物品详情页面
  onItemTap: function(e){
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if(!is_Register){
      wx.showModal({
        title: '友情提示',
        content: '请先注册',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }
        }
      })
    }else{
      var itemId = e.currentTarget.dataset.item_id;
      wx.navigateTo({
        url: '/pages/detail/detail?itemId=' + itemId,
      })
    }
  },
 
 //向下自动延伸出商品
  onReachBottom: function () {
    var that = this;
    var page_cnt = that.data.page_cnt + 10;
    this.setData({
      page_cnt : page_cnt
    })
    if (that.data.navbarActiveIndex == 1) that.readItemList_browse(that.data.navbarActiveIndex);
    else that.readItemList(that.data.navbarActiveIndex);
  },

  //热度排名商品
  readItemList_browse: function (navbarActiveIndex) {
    var that = this;
    var is_campus = wx.getStorageSync('is_campus');
    wx.request({
      url: app.globalData.targetUrl + 'items/',
      data: {
        page_cnt: that.data.page_cnt,
        page: that.data.nowPage,
      },
      success: function (res) {
        var items = res.data.data;
        for(var i = 0; i < items.length; i++){
          for(var j = i+1; j < items.length; j++){
            if(items[i].view_cnt < items[j].view_cnt){
              var item = items[i];
              items[i] = items[j];
              items[j] = item;
            }
          }
        }
        that.setData({
          items: items,
          navbarActiveIndex: navbarActiveIndex
        })
      }
    })
  },
  todiandian:function(e){
    var that=this;
    var imageid = e.currentTarget.dataset.id;
    if (imageid==1){
      wx.navigateTo({
        url: '/pages/aboutdiandian/showdiandian'
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/aboutdiandian/aboutdiandian'
      })
    }

  },
  
  onNavBarTap02: function (e) {
    var that=this;
    var navbarActiveIndex = e.currentTarget.dataset.navbarindex;
    if (navbarActiveIndex == 1) that.readItemList_browse(navbarActiveIndex);
    else that.readItemList(navbarActiveIndex);
  }
})